
//
//  GesterLikeViewController.m
//  GoShops
//
//  Created by zcj on 15-12-1.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "GesterLikeViewController.h"
#import "Grouble.h"
#import "SelectFindHttp.h"
#import "GetUrlString.h"
#import "GesterRecommModel.h"
#import "UITableView+JFTableView.h"
#import "ShopGesterHomeRecommModel.h"
#import "FirstGesterTableViewCell.h"
#import "GesterLikeRecomTableViewCell.h"
#import "ReturnGoodTableViewCell.h"
#import "BackWebViewController.h"


@interface GesterLikeViewController ()<UITableViewDataSource,UITableViewDelegate,ReturnGoodTableViewCellDelegate>
@property (atomic,strong)NSMutableArray * gesterMutable;
@property(nonatomic, strong)UITableView *firstTableView;
@property (nonatomic,strong)NSMutableArray * gesterLikeMutable;

@end

@implementation GesterLikeViewController
- (NSMutableArray *)gesterLikeMutable
{
    if (_gesterLikeMutable == nil) {
        _gesterLikeMutable = [NSMutableArray array];
    }
    return _gesterLikeMutable;
}
- (NSMutableArray *)gesterMutable
{
     if (_gesterMutable == nil) {
        _gesterMutable = [NSMutableArray array];
    }
    return _gesterMutable;
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    ZCJLog(@"%@",self.ppoid);
    self.view.backgroundColor = [UIColor whiteColor];
    [self iniTitle];
    [self initData];
    [self initTableView];
}
- (void)iniTitle
{
    self.title = @"详情";
    
}

- (void)initTableView
{
    self.firstTableView = [UITableView initWithTableView:CGRectMake(0, 0, ScreenWidth, ScreenHeight) withDelegate:self];
    self.firstTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.firstTableView];
}
- (void)initData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 访问一个对象加锁
        //@synchronized(self.gesterMutable){
            
            [self loadShopData];
            [self loadData];
        //}
        
    });
}
- (void)loadShopData
{
    NSString *urlStr =  [[GetUrlString sharedManager]urlWithShopDetailData:self.ppoid];
    ZCJLog(@"%@",urlStr);
    [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
        NSMutableArray * dataArray = [json objectForKey:@"data"];
        shopDetailModel = [ShopGesterHomeRecommModel objectWithKeyValues:dataArray[0]];
        [self.gesterLikeMutable addObject:shopDetailModel];
        
    } failture:^(id error) {
        ZCJLog(@"%@",error);
        [MBProgressHUD showError:@"头部加载失败..."];
    }];
    
}
- (void)loadData
{
     NSString *urlStr =  [[GetUrlString sharedManager]urlWithOtherRecommendShop:self.ppoid];
    ZCJLog(@"%@",urlStr);
    [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
        NSDictionary *dataDic = [json objectForKey:@"data"];
        NSMutableArray *dealsArray = [dataDic objectForKey:@"deals"];
        [self.gesterMutable removeAllObjects];
        for (int i = 0; i < dealsArray.count; i++) {
            GesterRecommModel *shopRM = [GesterRecommModel objectWithKeyValues:dealsArray[i]];
            [self.gesterMutable addObject:shopRM];
        }
        [self.firstTableView reloadData];
        
        //[self.firstTableView reloadData];
    } failture:^(id error) {
        
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1 + self.gesterMutable.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        FirstGesterTableViewCell * cell = [FirstGesterTableViewCell FirstGesterTableViewCell:tableView];
        if(self.gesterLikeMutable.count != 0){
            cell.shopGesterRecommModel = self.gesterLikeMutable[0];
            
        }
        return cell;
    }else if (indexPath.row == 1){
        ReturnGoodTableViewCell * cell =[ReturnGoodTableViewCell ReturnGoodTableViewCell:tableView];
        cell.delegate = self;
        return cell;
    }else{
        GesterLikeRecomTableViewCell * cell = [GesterLikeRecomTableViewCell GesterLikeRecomTableViewCell:tableView];
        if (self.gesterMutable.count != 0) {
            cell.gesterModel = self.gesterMutable[indexPath.row - 2];
        }
        return cell;
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 165;
    }else if (indexPath.row == 1){
        return 44;
        }
    return 120;
}

#pragma mark - 退货按钮跳转控制器
- (void)ReturnGoodTableViewCellBtnClick
{
    NSString *urlStr =  [[GetUrlString sharedManager]urlBackAnyTimeWebData];
    BackWebViewController *webVC = [[BackWebViewController alloc]init];
    webVC.urlStr = urlStr;
    [self.navigationController pushViewController:webVC animated:YES];
}
@end

